//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.model.vo
{
	import __AS3__.vec.Vector;
	
	import mx.formatters.DateFormatter;
	
	import reader.core.ReaderModelLocator;
	import reader.core.ReaderResource;
	import reader.utils.StringUtils;
	
	public class BlogItemVO extends SelectAbleVO
	{
		//id
		[Bindable] public var blogId		:int = -1;
		[Bindable] public var blogSourceId	:int = -1;
		
		//feed variables
		[Bindable] public var title			:String;
		[Bindable] public var authors		:String;
		[Bindable] public var link			:String;
		[Bindable] public var guid			:String;
				
		//list display variables
		[Bindable] public var isStar		:Boolean;
		[Bindable] public var isRead		:Boolean;
		[Bindable] public var isKeepUnread	:Boolean;
		
		//private
		
		private static var format		:DateFormatter = new DateFormatter();
		private var resource			:XML = ReaderResource.instance.resource;
		
		public function BlogItemVO():void
		{
			format.formatString = "YYYY-M-D";
		}
		
		//----------------------------------------------------------------------------
		//
		// description
		//
		//----------------------------------------------------------------------------
		
		private var _trimHtmlDescription:String;
		private var _description		:String;
		private var _HTMLDescription	:String;
		[Bindable]
		public function set description(value:String):void
		{
			_description = value;
			var desc:String = StringUtils.removeExtraWhitespace(_description.replace(/<[^>].*?>/g,""));
		    if(desc.length > ReaderModelLocator.MAX_SUMMARY_TEXT)
		    	desc = desc.substr(0, ReaderModelLocator.MAX_SUMMARY_TEXT) + "...";
		    _trimHtmlDescription = desc;
		    _HTMLDescription = ReaderModelLocator.HTML_HEADER + description + ReaderModelLocator.HTML_ROOT;
		}
		public function get description():String
		{
			return _description;
		}
		public function get trimHtmlDescription():String
		{    
			return _trimHtmlDescription;
		}
		public function get HTMLDescription():String
		{
			return _HTMLDescription;
		}
		
		//----------------------------------------------------------------------------
		//
		// published date
		//
		//----------------------------------------------------------------------------
		
		private var _pubDate	:Date;
		private var _date		:String;
		[Bindable]
		public function set pubDate(value:Date):void
		{
			_pubDate = value;
			_date = formatDate(_pubDate, resource);
		}
		public function get pubDate():Date
		{
			return _pubDate ? _pubDate : new Date();
		}	
		public function get date():String
		{
			return _date;
		}
		public function get time():Number
		{
			return pubDate.getTime();
		}
		
		//----------------------------------------------------------------------------
		//
		// created date, time of insert into database
		//
		//----------------------------------------------------------------------------
		
		private var _creatDate		:Date;
		private var _creatTime		:String;
		[Bindable]
		public function get creatDate():Date
		{
			return _creatDate;
		}
		public function set creatDate(value:Date):void
		{
			_creatDate = value;
			_creatTime = formatDate(_creatDate, resource);
		}
		
		public function get updateTime():String
		{
			return _creatTime;
		}
		//----------------------------------------------------------------------------
		//
		// large sqlresult process.
		//
		//----------------------------------------------------------------------------
		public var isFoolMode:Boolean;
		protected var _sourceObject:Object;
		public function set sourceObject(value:Object):void
		{
			_sourceObject = value;
			isFoolMode = true;
		}
		public function get sourceObject():Object
		{
			return _sourceObject;
		}
		public function resetSourceObject2Item():void
		{
			if(isFoolMode)
			{
				blogId = _sourceObject.blogId;
				blogSourceId = _sourceObject.blogSourceId;
				title = _sourceObject.title;
				authors = _sourceObject.authors;
				link = _sourceObject.link;
				description = _sourceObject.description;
				pubDate = _sourceObject.pubDate;
				isRead = _sourceObject.isRead;
				isStar = _sourceObject.isStar;
				isKeepUnread = _sourceObject.isKeepUnread;
				creatDate = _sourceObject.creatDate;
				isFoolMode = false;
				_sourceObject = null;
			}
		}
				
		//----------------------------------------------------------------------------
		//
		// methods
		//
		//----------------------------------------------------------------------------
		
		public function toString():String
		{
			return "[BlogItemVO] title=" + title;
		}
		
		public static function getLatestBlogItem(data:Number, blogs:Vector.<BlogItemVO>):Vector.<BlogItemVO>
		{
			var a:Vector.<BlogItemVO> = new Vector.<BlogItemVO>();
						
			for each(var blog:BlogItemVO in blogs)
			{
				if(blog.pubDate.getTime() > data)
					a.push(blog);
				else
					break;
				
			}
			return a;
		}
		
		public static function sortBlogByPubDate(x:BlogItemVO, y:BlogItemVO):Number
		{
			return y.time - x.time;
		}
		
		public static function sourceObject2BlogItem(obj:Object):BlogItemVO
		{
			var blog:BlogItemVO = new BlogItemVO();
			blog.blogId = obj.blogId;
			blog.blogSourceId = obj.blogSourceId;
			blog.title = obj.title;
			blog.authors = obj.authors;
			blog.link = obj.link;
			blog.description = obj.description;
			blog.pubDate = obj.pubDate;
			blog.isRead = obj.isRead;
			blog.isStar = obj.isStar;
			blog.isKeepUnread = obj.isKeepUnread;
			blog.creatDate = obj.creatDate;
			return blog;
		}
		
		//----------------------------------------------------------------------------
		//
		// format mathods
		//
		//----------------------------------------------------------------------------
		
		public static function formatDate(date:Date, resource:XML):String
		{
			var s:String = "";
			var d:Date = new Date();
			if(date)
			{
				var t:int = (d.getTime() - date.getTime()) / 1000;
				if(t < 0)
					s = getHourMin(date, resource) + "(" + t + resource.blogItemDateISec + ")";
				if(t < 60)
					s = getHourMin(date, resource) + "(" + resource.blogItemDateSec + ")";
				if( t >= 31104000)	
					s = getMonnth(date);
				if( t >= 2592000)
					s = getMonnth(date) + "(" + (int(t / 2592000)+1) + resource.blogItemDateMon + ")";
				if( t < 2592000)
				{
					var td:int = int(t / 86400) + 1;
					if(td == 1)
						s = getMonnth(date) + "(" + resource.blogItemDateYDay + ")";
					else if(td == 2)
						s = getMonnth(date) + "(" + resource.blogItemDateBDay + ")";	
					else
						s = getMonnth(date) + "(" + td + resource.blogItemDateDay + ")";	
				}					
				if( t < 86400)
					s = getHourMin(date, resource) + "(" + (int(t / 3600) + 1) + resource.blogItemDateHour + ")";
				if( t < 3600)
					s = getHourMin(date, resource) + "(" + (int(t / 60) + 1) + resource.blogItemDateMin + ")";									
			}
			return s;
		}
		
		//private
		
		private static function getHourMin(d:Date, resource:XML):String
		{
			var ampm:String = (d.hours > 11)? resource.blogItemDateAfternoon : resource.blogItemDateMorining;
			var hour:int = 0;
			
			var h:int = d.hours;			
			if(h == 0 || h == 12)
			{
				hour = 12;
			}
			else if(h > 12)
			{
				hour = h - 12;
			}
			else
			{
				hour = h;
			}			
			return ampm + hour + ":" + d.minutes;
		}
		
		private static function getMonnth(d:Date):String
		{			
			return format.format(d);
		}
		
		//----------------------------------------------------------------------------
		//
		// Convert Vector to Array
		//
		//----------------------------------------------------------------------------
		
		public static function VectorToArray(v:Vector.<BlogItemVO>):Array/* of BlogItemVO */
		{
			var a:Array = [];
			for each(var i:BlogItemVO in v)
			{
				a.push(i);
			}
			return a;
		}
		
	}
}